package cn.ac.aircas.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.ac.aircas.common.core.redis.RedisCache;
import cn.ac.aircas.common.utils.uuid.IdUtils;
import cn.ac.aircas.system.mapper.SysConfigMapper;
import cn.ac.aircas.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.ac.aircas.common.constant.Constants;
import cn.ac.aircas.common.core.domain.AjaxResult;
import cn.ac.aircas.common.core.domain.entity.SysMenu;
import cn.ac.aircas.common.core.domain.entity.SysUser;
import cn.ac.aircas.common.core.domain.model.LoginBody;
import cn.ac.aircas.common.utils.SecurityUtils;
import cn.ac.aircas.framework.web.service.SysLoginService;
import cn.ac.aircas.framework.web.service.SysPermissionService;
import cn.ac.aircas.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author defected
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();

        Map<String,Object> resData = new HashMap<>();

        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(), loginBody.getRegionId());

        SysUser sysUser = userService.selectUserByUserName(loginBody.getUsername());
        boolean pwdExpire = loginService.isPwdExpire(sysUser);
        if (pwdExpire) {
            resData.put("res_code", 1001);
            String signKey = Constants.RESET_SIGN_KEY + loginBody.getUsername();
            String signCode = IdUtils.fastSimpleUUID();
            redisCache.setCacheObject(signKey, signCode, Constants.RESET_EXPIRATION, TimeUnit.MINUTES);
            resData.put("reset_sign", signCode);
        }

        resData.put(Constants.TOKEN, token);
        ajax.put("data", resData);
        return ajax;
    }

    /**
     * TODO 密评改造
     * @param loginBody
     * @return
     */
    @PostMapping("/loginHmac")
    public AjaxResult loginHmac(@RequestBody LoginBody loginBody) {

        return login(loginBody);
    }

    //TODO 获取随机数
    @GetMapping("getUKeyRandomNum")
    public AjaxResult getRandomNum() {
        AjaxResult ajax = AjaxResult.success();
        Map<String, Object> res = new HashMap<>();
        res.put("randomNum", IdUtils.fastSimpleUUID());
        ajax.put("data", res);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        // TODO SSO登陆  resTemplate拿取用户信息、使用permissionService.getSSOMenuPermission()获取角色集合
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // TODO  根据token拿用户信息 并固定 角色集合   roles=“common”
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
